const { Socket } = require('dgram');
const net = require('net');
const readline = require('readline');

const client = new net.Socket();
const port = 9527;
const host = 'localhost';

client.connect(port, host, () => {
    client.write('hello.');
})

client.on('data', (data) => {
    console.log(data.toString());
    say();
})
client.on('close', (data) => {
    console.log('connection closeed');
})
client.on('error', (err) => {
    throw err;
})

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

function say() {
    rl.question('请输入：', (answer) => {
        if (answer != 'bye' ) {
            client.write(answer);
        } else {
            client.destroy();
            rl.close();
        }
    });
}